package cn.valo.basic.controller;

import cn.valo.basic.dto.RegisterDto;
import cn.valo.basic.service.IVerifyCodeService;
import cn.valo.basic.util.AjaxResult;
import cn.valo.basic.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCode {
    @Autowired
    private IVerifyCodeService verifyCodeService;
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult img(@PathVariable("imageCodeKey") String key){
        try {
            String base64Str = verifyCodeService.imageCode(key);
            return AjaxResult.success().setResultObj(base64Str);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
     * 发送短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/smscode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto ){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.success();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
        }
    }

}
